﻿// Replace the following with your custom JavaScript. 
var ctx;
var web;
var user;
//settings
var settingsList;
var photoList;
var thumbnailCount;
var settingscamlQuery;



function sharePointReady() {
    this.ctx = new SP.ClientContext.get_current();
    getSettings();
}




function getSettings() {
    var oList = ctx.get_web().get_lists().getByTitle('Settings');
    var camlQuery = new SP.CamlQuery();
    //camlQuery.set_viewXml('<View><RowLimit>1</RowLimit></View>');
    camlQuery.set_viewXml('<View><Query><Where><Eq><FieldRef Name=\'FullScrollableInstance\' /><Value Type=\'Text\'>Home</Value></Eq></Where></Query><RowLimit>100</RowLimit></View>');
    settingsList = oList.getItems(camlQuery);
    ctx.load(settingsList);
    ctx.executeQueryAsync(Function.createDelegate(this, this.loadSettingsSuccess), Function.createDelegate(this, this.loadSettingsFail));

}
function loadSettingsSuccess(sender, args) {
    var listItemInfo = '';
    var listItemEnumerator = this.settingsList.getEnumerator();

    while (listItemEnumerator.moveNext()) {
        var oListItem = listItemEnumerator.get_current();
        thumbnailCount = oListItem.get_item('FullScrollableDisplayThumbnails');
        settingscamlQuery = oListItem.get_item('FullScrollableCAMLQuery');
    }

    $(".starter").append('thumbnail: ' + this.thumbnailCount + 'camlquery: ' + settingscamlQuery);

    getListItems();
}
function loadSettingsFail(sender, args) {
    alert('Request failed. ' + args.get_message() +
        '\n' + args.get_stackTrace());
}



function getListItems() {
    var oWebsite = ctx.get_web();
    var collList = oWebsite.get_lists().getByTitle('SPGPhotoList');
    var camlQuery = new SP.CamlQuery();
    camlQuery.set_viewXml(settingscamlQuery);
    this.collListItem = collList.getItems(camlQuery);
    ctx.load(collListItem);
    ctx.executeQueryAsync(Function.createDelegate(this, this.onPhotoQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
}
function onPhotoQuerySucceeded() {
    var counter = 0;
    var insertDiv = '';
    var insertHiddenDiv = '';
    var strImage;

    var listItemEnumerator = collListItem.getEnumerator();
    while (listItemEnumerator.moveNext()) {
        var oListItem = listItemEnumerator.get_current();
        if (counter == 0) {
            insertDiv += '<div>';
        }
        //insert stuff here
        strImage = oListItem.get_item('FileDirRef');
        strImage += '/' + oListItem.get_item('FileLeafRef');
        strImage = '<img src="' + strImage + '"' + ' alt="' + oListItem.get_item('FileLeafRef') + '"/>';
        insertDiv += strImage;

        insertDiv += '<div id="itemdetailthumbnail">';
        insertDiv += 'Posted ' + oListItem.get_item('Modified').getMonth() + '/' +
            oListItem.get_item('Modified').getDate() + '/' +
            oListItem.get_item('Modified').getFullYear();
        insertDiv += '<br/>';
        insertDiv += 'By ' + oListItem.get_item('Editor').get_lookupValue();
        insertDiv += '</div>';

        if (counter == this.thumbnailCount) {            //this is where I want to close the div tag
            insertDiv += '</div>';
            counter = -1;
        }
        else if (counter == collListItem.get_count()) {
            insertDiv += '</div>';
        }

        counter = counter + 1;

    }
    $(".scrollable .items").append(insertDiv);

    //need to iterate again and create the hidden container
    var listItemEnum2 = collListItem.getEnumerator();
    while (listItemEnum2.moveNext()) {
        var oListItem = listItemEnum2.get_current();

        var filename = oListItem.get_item('FileLeafRef');

        strImage = oListItem.get_item('FileDirRef');
        strImage += '/' + oListItem.get_item('FileLeafRef');
        strImage = '<img src="' + strImage + '"' + ' alt="' + filename + '"/>';

        filename = filename.substring(0, filename.indexOf('.'));
        insertHiddenDiv += '<div class="itemshidden" id="' + filename + '">';
        //open div

        insertHiddenDiv += strImage;
        insertHiddenDiv += '<strong>' + oListItem.get_item('SPGDescHeader') + '</strong>';
        insertHiddenDiv += '<p>' + oListItem.get_item('SPGParagraphText') + '</p>';
        insertHiddenDiv += '<a href="' + oListItem.get_item('SPGDisplayHyperLink').get_url() + '">For more info click here!</a>';


        //close div
        insertHiddenDiv += '</div>';
    }
    $(".itemshiddencontainer").append(insertHiddenDiv);

    $('.thumbnailcontainer .scrollable .items img').first().click();
    $(".thumbnailcontainer .scrollable").scrollable();
}
function onQuerySucceeded() {
    var listItemInfo = '';
    var listItemEnumerator = collListItem.getEnumerator();
    while (listItemEnumerator.moveNext()) {
        var oListItem = listItemEnumerator.get_current();
        listItemInfo += '\nID: ' + oListItem.get_id() +
            ' \nTitle: ' + oListItem.get_item('Title') +
            ' \nModified day: ' + oListItem.get_item('Modified').getDate() +
            ' \nModified month: ' + oListItem.get_item('Modified').getMonth() +
            ' \nModified year: ' + oListItem.get_item('Modified').getFullYear() +
            ' \nEdited by: ' + oListItem.get_item('Editor').get_lookupValue() +
            ' \nParagraph: ' + oListItem.get_item('SPGParagraphText') +
            ' \nPublish: ' + oListItem.get_item('SPGPublish') +
            ' \nURL: ' + oListItem.get_item('SPGDisplayHyperLink').get_url() +
            ' \nimagename: ' + oListItem.get_item('FileLeafRef') +
            ' \nfull path: ' + oListItem.get_item('FileDirRef');
    }
    $(".scrollable .items").append(listItemInfo.toString());
}

function onQueryFailed(sender, args) {
    alert('Request failed. ' + args.get_message() +
        '\n' + args.get_stackTrace());
}
